using System;
using System.Web;
using System.Web.Mvc;
using Tools.Common.Reflection;
using Web.App.Infrastructure.Interface;
using Web.App.Infrastructure.Interface.Contracts;
using Web.App.Infrastructure.Localization.Contract;
using Web.App.Infrastructure.Security.Contracts;
using Webshop20.Application;
using Webshop20.Application.Operations.Messages;
using Webshop20.Application.Operations.Services;
using Webshop20.Application.Queries.Messages;
using Webshop20.Application.Queries.Services;
using Webshop20.Presentation;

namespace Webshop20.Interface.MVC.Controllers
{
    public class CartController : ControllerBase
    {
        private readonly IIdentityProvider identityProvider;
        private readonly ICookieRepository cookieRepository;
        private readonly IAddProductToCartService addProductToCartService;
        private IRemoveProductFromCartService removeProductFromCartService;
        private IGetCartService getCartService;  
        private ICookieFactory cookieFactory;

        public CartController(ILocalisationService localisationService, IAddProductToCartService addProductToCartService, ICookieFactory cookieFactory, IRemoveProductFromCartService removeProductFromCartService, IGetCartService getCartService) : base(localisationService)
        {
            this.identityProvider = identityProvider;
            this.addProductToCartService = addProductToCartService;
            this.getCartService = getCartService;
            this.removeProductFromCartService = removeProductFromCartService;
            this.cookieFactory = cookieFactory;
            this.cookieRepository = cookieRepository;
        }

        public ActionResult Add(AddProductToCartRequest request, ControllerRedirect redirect)
        {
            //int visitorId;
            //Guid? anonymousVisitorId = null;

            //int.TryParse(ControllerContext.HttpContext.User.Identity.Name, out visitorId);
            //if (ControllerContext.HttpContext.Request.Cookies["AnonymousVisitorId"] != null)
            //    anonymousVisitorId = new Guid(ControllerContext.HttpContext.Request.Cookies["AnonymousVisitorId"].Value);


            var response = addProductToCartService.Handle(request) as AddProductToCartResponse;

            if (response.VisitorInterimKey != null)
                ControllerContext
                    .HttpContext
                    .Response
                    .Cookies
                    .Add(
                            cookieFactory.CreateCookie(ReflectAbout<RequestMessageBase>.GetProperty(x=>x.AnonymousVisitorId).Name,
                            response.VisitorInterimKey.ToString(), DateTime.Now.AddDays(2)));
            
            return RedirectToAction(redirect.ControllerAction, redirect.ControllerName, redirect.ActionParameters);
        }



        public ActionResult Remove(RemoveProductFromCartRequest request)
        {
            removeProductFromCartService.Handle(request);
            return RedirectToAction<CartController>(x=>x.Overview(null), new { request = new GetCartRequest(request.AnonymousVisitorId, request.VisitorId, DateTime.Now) });
        }

        public ActionResult Overview(GetCartRequest request)
        {
            var response = getCartService.Handle(request) as GetCartResponse;
            return View(new ShoppingCart(response.TotalPrice.ToString(), response.SavedItems, response.PurchasedItems));
        }
    }
}